package org.example.movie;


import org.example.RtspInfo;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Scalar;
import org.opencv.imgproc.Imgproc;
import org.opencv.videoio.VideoCapture;


/**
 * @author zehua
 * @date 2023/11/8 17:20
 * @Description TODO 检测花屏
 * @since V1.1.0
 */
public class VideoCheck {

    static {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    }

    public static void main(String[] args) {
        VideoCapture capture = new VideoCapture("E:\\test\\video\\test_2023-11-081654652.mp4");
        Mat frame = new Mat();
        Mat prevFrame = new Mat();
        Mat diffFrame = new Mat();

        if (capture.isOpened()) {
            while (true) {
                boolean hasFrame = capture.read(frame);

                if (!hasFrame) {
                    break;
                }

                if (!prevFrame.empty()) {
                    // 转换为灰度图
                    Imgproc.cvtColor(frame, frame, Imgproc.COLOR_BGR2GRAY);
                    Imgproc.cvtColor(prevFrame, prevFrame, Imgproc.COLOR_BGR2GRAY);

                    // 计算当前帧和前一帧的差异
                    Core.absdiff(prevFrame, frame, diffFrame);
                    Scalar diff = Core.sumElems(diffFrame);

                    double totalDiff = diff.val[0] + diff.val[1] + diff.val[2];
                    System.out.println(totalDiff);
//                    if (totalDiff > THRESHOLD) {
                        // 如果差异大于某个阈值，可能意味着花屏
                        System.out.println("可能出现花屏！");
//                    }
                }

                // 更新前一帧
                frame.copyTo(prevFrame);
            }
        }

        capture.release();
    }
}
